<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="JavaBean.users" %>
<%@ page language="java" contentType="text/html;charset=utf-8"%>
<html>
  <head>
    <title>登陆处理页面</title>
  </head>
  <body>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String referer = request.getHeader("Referer");
if(referer==null||referer.trim().length()==0){
response.sendRedirect("index.jsp");
return;
}
String name=request.getParameter("username");
String pwd=request.getParameter("password");
    System.out.println(name+pwd);
    System.out.println(username+password);

%>
<%!
String url = "jdbc:mysql://localhost:3306/jdbc1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
String username = "root";
String password = "Sxxy@2018";
%>
<%
    Connection conn = DriverManager.getConnection(url,username,password);//得到连接
    //查询出userID
    PreparedStatement pstmt = conn.prepareStatement("select id,username,password,gender,email,resume,isadmin from tb_user where username=? AND password = ?");
    pstmt.setString(1,name);
    pstmt.setString(2,pwd);
    ResultSet rs = pstmt.executeQuery();
    if(rs.next()) {
        //获取当前登录用户的管理员状态,并封装到对象里面
        users users = new users();
        //封装好是否为管理员和该用户的ID
        users.setId(rs.getInt("id"));
        users.setName(rs.getString("username"));
        users.setGender(rs.getString("gender"));
        users.setEmail(rs.getString("email"));
        users.setResume(rs.getString("resume"));
        users.setIsAdmin(rs.getString("isadmin"));

        //获取到当前刚刚封装的数据
        String isAdmin = users.getIsAdmin();
        int id = users.getId();
        System.out.println("isAdmin = " + isAdmin);
        //判断是否是管理员,当为null的时候分别跳转到管理员或普通用户页面
        if(isAdmin.trim().equals("是")) {
            response.sendRedirect("./adminstrator/index.html");
        } else{

            HttpSession session1 = request.getSession();
            session1.setAttribute("login_user",users);
//            session.setAttribute("userID",id);
//            session.setAttribute("uName", name);
            response.sendRedirect("index2.jsp");
//            response.sendRedirect("/adminstrator/booksManager/doEditUserBooksIN.jsp");
//            response.sendRedirect("/adminstrator/booksManager/userBooksManager.jsp");
        }
    }
        else{
//            session.setAttribute("usertxt", "用户名或密码错误！！！");
            response.sendRedirect("loginRegister.jsp");
    }

//判断登陆界面的用户名和密码，根据输入的不同情况进行不同的处理
%>
</body>

</html>

